package com.romejiang;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.resource.Resource;


/**
 * Simple Jetty FileServer.
 * This is a simple example of Jetty configured as a FileServer.
 */
public class FileServer
{
    public static void main(String[] args) throws Exception
    {
//        System.setProperty("org.eclipse.jetty.LEVEL","INFO");
        // Create a basic Jetty server object that will listen on port 8080.  Note that if you set this to port 0
        // then a randomly available port will be assigned that you can either look in the logs for the port,
        // or programmatically obtain it for use in test cases.
//        Server server = new Server(8080);

        // Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
        // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
//        ResourceHandler resource_handler = new ResourceHandler();
//
//        // Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
//        // In this example it is the current directory but it can be configured to anything that the jvm has access to.
//        resource_handler.setDirectoriesListed(true);
//        resource_handler.setWelcomeFiles(new String[]{ "index.html" });
//        resource_handler.setResourceBase("/Users/stephen/Projects/java/jetty2/");
//
//        ServletContextHandler apiHandler = new ServletContextHandler();
//        String apiResourceBase = "/Users/stephen/Projects/java/jetty2/";
//        if (apiResourceBase != null) {
//            ServletHolder defaultServletHolder = new ServletHolder(new DefaultServlet());
//
//            defaultServletHolder.setInitParameter("resourceBase",    apiResourceBase);
//            defaultServletHolder.setInitParameter("dirAllowed",      "false");
//            defaultServletHolder.setInitParameter("welcomeServlets", "true");
//            defaultServletHolder.setInitParameter("redirectWelcome", "true");
//            defaultServletHolder.setInitParameter("gzip", "true");
//            apiHandler.addServlet(defaultServletHolder, "/*");
//            apiHandler.setWelcomeFiles(new String[]{"index.html"});
//
////            System.out.println(apiHandler.getBaseResource().getURI());
//            System.out.println(apiHandler.getContextPath());
//
//        }


        // Add the ResourceHandler to the server.
//        HandlerList handlers = new HandlerList();
//        handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
//        server.setHandler(handlers);


//        Server server = new Server(8080);
//
//
//        ResourceHandler resource_handler = new ResourceHandler();
//        resource_handler.setDirectoriesListed(true);
//        resource_handler.setWelcomeFiles(new String[]{ "index.html" });
//
//        resource_handler.setResourceBase(".");
//
//        HandlerList handlers = new HandlerList();
//        handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
//        server.setHandler(handlers);
//
//        server.start();
//        server.join();


        Server server = new Server(8080);

        ServletContextHandler ctx = new ServletContextHandler();
        ctx.setContextPath("/");

        DefaultServlet defaultServlet = new DefaultServlet();
        ServletHolder holderPwd = new ServletHolder("default", defaultServlet);
        holderPwd.setInitParameter("resourceBase", "/Users/stephen/Projects/java/jetty2/");

        ctx.addServlet(holderPwd, "/*");
//        ctx.addServlet(InfoServiceSocketServlet.class, "/info");

        server.setHandler(ctx);


        // The filesystem paths we will map
//        String homePath = "/Users/stephen/Projects/java/jetty2/";
//        String pwdPath = homePath;
//
//        // Setup the basic application "context" for this application at "/"
//        // This is also known as the handler tree (in jetty speak)
//        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
//        context.setBaseResource(Resource.newResource(pwdPath));
//        context.setContextPath("/");
//        server.setHandler(context);
//
//
//
//        // add special pathspec of "/home/" content mapped to the homePath
//        ServletHolder holderHome = new ServletHolder("static-home", DefaultServlet.class);
//        holderHome.setInitParameter("resourceBase",homePath);
//        holderHome.setInitParameter("dirAllowed","true");
//        holderHome.setInitParameter("pathInfoOnly","true");
//        context.addServlet(holderHome,"/home/*");
//
//        // Lastly, the default servlet for root content (always needed, to satisfy servlet spec)
//        // It is important that this is last.
//        ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
//        holderPwd.setInitParameter("dirAllowed","true");
//        context.addServlet(holderPwd,"/");

        try
        {
            server.start();
            server.dump(System.err);
            server.join();
        }
        catch (Throwable t)
        {
            t.printStackTrace(System.err);
        }
    }
}